home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Libraries / VideoToolbox 94.11.17 / VideoToolboxSources / MoveMouse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-15  |  10.3 KB  |  247 lines  |  [TEXT/KAHL]

  1. /*
  2. MoveMouse.c
  3.  
  4. (This is from Apple. It may be useful to people who need
  5. an equivalent to SetMouse.c that works on the Centris
  6. Macs. Unfortunately it can't be compiled to native code
  7. for PowerPC. Most people should stick to the current
  8. SetMouse.c and ignore this file and the Cursor Device
  9. Manager until Apple provides us with a truly general
  10. interface that works on ALL Macs. - dgp, 10/94
  11.  
  12. NOTE: The THINK C 6 compiler chokes on the (legal) recursive structure
  13. definition at line 86. The THINK C 7 and CodeWarrior compilers don't 
  14. complain.)
  15.  
  16. From Apple Developer Services:
  17.  
  18. In the past, moving the cursor programatically on the Macintosh has
  19. entailed the manipulation of low memory globals. This has worked for
  20. all macs for quite some time. But with the advent of the power book
  21. series and many 3rd party track balls, it has become apparent that the
  22. low memory globals no longer contained adequate information for the
  23. system to truly support all types of positioning devices. So, with the
  24. new Centris CPU's, and CPU's from all lines after we are implementing a
  25. new tool set that provides a much more comprehensive set of utilities
  26. for dealing with the cursor location and mouse button state. We call
  27. this new manager the Cursor Device Manager, and DTS will be documenting
  28. it in a future tech not, but since many machines are now shipping that
  29. do not use the traditional low memory globals to get the mouse location
  30. and position the mouse, it is important to "leak" a little of the tech
  31. note information early.
  32.  
  33. First, the CDM (Cursor device manager) has its own trap ($AADB) and you
  34. can determine if a machine supports the CDM by using the standard trap
  35. Available routine. (See inside mac for the source code to Trap
  36. Available). Once you have determined that the CDM exists, you should
  37. try not to use the low memory globals RawMouse and MTemp to get the
  38. current cursor location, or set a new cursor location. Instead the CDM
  39. provides 3 calls which you can use to get this info, the first is
  40. CrsrDevNextDevice which will give you the next cursor device record in
  41. the CDM list, if you pass it a NULL, it will give you the head of the
  42. list. Once you have the first cursor device you can use it to get the
  43. Cursor data record which contains the current cursor location in its
  44. where field. You can set the current location with either the
  45. CrsrDevMove (which moves relative to the current location) or the
  46. CrsrDevMoveTo (which moves to an absolute location) routines.
  47.  
  48. The following is some sample code which implement some generic routines
  49. that work on all macintoshes. These routines are written not for speed
  50. but for clarity, so if you are writing a device driver or time critical
  51. code there are some things that you can do to speed up these routines,
  52. like predetermine at the outset if the CDM exists, this way you
  53. wouldn't have to check every time you want to use it, an d getting the
  54. cursor device only once and keeping the pointer to it around between
  55. calls.
  56.  
  57. Anyway, here is some sample code that moves the mouse diagonally until
  58. the mouse button is pressed. For a further explanation of how the older
  59. low memory global method works, see the developer support tech info
  60. database on AppleLink.
  61.  
  62. NOTE: Despite all the above claims to providing a general solution,
  63. in fact this code is 680x0-specific, and can't be compiled to run
  64. native on PowerPC. So I'm not planning on using the Cursor Device
  65. Manager until Apple provides a platform-independent calling interface.
  66.  
  67. HISTORY:
  68. 8/94 dgp Downloaded from ftp://nada.kth.se/pub/hacks/mac-faq/MoveMouse.c
  69. 10/94 dgp made minimal changes so that it would compile in CodeWarrior C. Still untested.
  70. */
  71.  
  72. #if __powerc
  73.     #error "This file cannot be compiled as PowerPC native code."
  74. #else
  75. #include "VideoToolbox.h"    // TrapAvailable
  76. #include <Quickdraw.h>
  77.  
  78. // These are some temporary headers to use with CDM until we come out with
  79. // the real interfaces. DO NOT count on the field names and call names to
  80. // remain the same in the released interfaces, this is strictly advanced info
  81. // which is a best guess.
  82. struct CrsrDataRec {
  83.     struct CrsrDataRec *nextCrsrData; // Linked list ptr to next one
  84.     Ptr displayInfo; // Future expansion
  85.     Fixed whereX; // fixed point cursor location
  86.     Fixed whereY;
  87.     Point where; // QD cursor location
  88.     Boolean isAbs; // true if the coords last used were absolute
  89.     char ButtonCount; // number of buttons currently pressed
  90.     short ScreenRes; //pixels per inch on current screen
  91. };
  92.  
  93. typedef struct CrsrDataRec CrsrDataRec;
  94.  
  95. struct CrsrDevRec {
  96.     struct CrsrDevRec *nextCrsrDev; // linked list ptr to next device
  97.     CrsrDataRec *whichCursor; // cursor data associated with device
  98.     long refCon; // device specific data
  99.     long unused; // reserved internal use
  100.     OSType devID; // See future tech note for desc
  101.     Fixed resolution; // See future tech note for desc
  102.     char devClass; // See future tech note for desc
  103.     char cntButtons; // See future tech note for desc
  104.     char spare1; // See future tech note for desc
  105.     char buttons; // See future tech note for desc
  106.     char buttonOp[8]; // See future tech note for desc
  107.     long buttonTicks[8]; // See future tech note for desc
  108.     long buttonData[8]; // See future tech note for desc
  109.     long doubleClickTime; // See future tech note for desc
  110.     Fixed acceleration; // See future tech note for desc
  111.     Ptr accelPoints; // See future tech note for desc
  112.     Fixed deltaX; // See future tech note for desc
  113.     Fixed deltaY; // See future tech note for desc
  114.     Fixed errorX; // See future tech note for desc
  115.     Fixed errorY; // See future tech note for desc
  116.     Fixed denom; // See future tech note for desc
  117.     Fixed spread; // See future tech note for desc
  118.     char newData; // See future tech note for desc
  119.     char spare2; // See future tech note for desc
  120. };
  121. typedef struct CrsrDevRec CrsrDevRec;
  122.     
  123. pascal OSErr CrsrDevMove(CrsrDevRec *ourDevice,long deltaX,long deltaY) =
  124.  {0x303C,0x0000,0xAADB}; // Move cursor relative
  125. pascal OSErr CrsrDevMoveTo(CrsrDevRec *ourDevice,long absX,long absY) =
  126.  {0x303C,0x0001,0xAADB}; // Move cursor absolute
  127. pascal OSErr CrsrDevFlush(CrsrDevRec *ourDevice) = {0x303C,0x0002,0xAADB};
  128. // flush the device cause its idle (dump accumulated error stuff
  129. pascal OSErr CrsrDevButtons(CrsrDevRec *ourDevice,char buttons) = {0x303C,
  130.  0x0003,0xAADB}; // See future tech note for desc
  131. pascal OSErr CrsrDevButtonDown(CrsrDevRec *ourDevice) = {0x303C,0x0004,0xAADB};
  132.  // See future tech note for desc
  133. pascal OSErr CrsrDevButtonUp(CrsrDevRec *ourDevice) = {0x303C,0x0005,0xAADB};
  134.  // See future tech note for desc
  135. pascal OSErr CrsrDevButtonOp(CrsrDevRec *ourDevice) = {0x303C,0x0006,0xAADB};
  136.  // See future tech note for desc
  137. pascal OSErr CrsrDevSetButtons(CrsrDevRec *ourDevice) = {0x303C,0x0007,0xAADB};
  138.  // See future tech note for desc
  139. pascal OSErr CrsrDevSetAcceleration(CrsrDevRec *ourDevice) = {0x303C,0x0008,
  140.  0xAADB}; // See future tech note for desc
  141. pascal OSErr CrsrDevDoubleTime(CrsrDevRec *ourDevice) = {0x303C,0x0009,0xAADB};
  142.  // See future tech note for desc
  143. pascal OSErr CrsrDevUnitsPerInch(CrsrDevRec *ourDevice) = {0x303C,0x000A,
  144.  0xAADB}; // See future tech note for desc
  145. pascal OSErr CrsrDevNextDevice(CrsrDevRec **ourDevice) = {0x303C,0x000B,
  146.  0xAADB}; // obtains the next device in the list
  147. pascal OSErr CrsrDevNewDevice(CrsrDevRec **ourDevice) = {0x303C,0x000C,0xAADB};
  148.  // See future tech note for desc
  149. pascal OSErr CrsrDevDisposeDevice(CrsrDevRec *ourDevice) = {0x303C,0x000D,
  150.  0xAADB}; // See future tech note for desc
  151.  
  152. pascal void CallCrsr(void) = {0x2078,0x08EE,0x4E90};
  153. /* that is MOVE.L jCrsrTask,A0
  154.     JSR (A0)
  155. */
  156. // Old style mouse moving equates for the low memory globals
  157. #define xRawMouse 0x082C    // low memory global that has current mouse loc
  158. #define xMTemp 0x0828 // low memory global that has current mouse loc
  159. #define xCrsrNew 0x08CE // set after you change mtemp and rawmouse
  160. #define xCrsrCouple 0x08CF // true if the cursor is tied to the mouse
  161.  
  162. #define _CrsrDevDispatch 0xAADB // new cursor device manager trap 
  163.  
  164. void GetMouseDevPosn(Point *currentPoint);
  165. void GetMouseDevPosn(Point *currentPoint)
  166. {
  167.     // This routine returns the current cursor location
  168.     CrsrDevRec *firstMouse;
  169.     if (TrapAvailable(_CrsrDevDispatch)) { 
  170.         // If we get here we have the CDM
  171.         firstMouse = NULL;    // start at head of cursor dev list
  172.         CrsrDevNextDevice(&firstMouse);    // get the next cursor device
  173.         // Now get the current cursor location from the Cursor data
  174.         *currentPoint = firstMouse->whichCursor->where;
  175.     } else {
  176.         // No CDM so we use the low memory global
  177.         *currentPoint = *(Point *)xRawMouse;
  178.     }
  179. }
  180.  
  181. void SetMouseDevPosn(Point newPoint);
  182. void SetMouseDevPosn(Point newPoint)
  183. {
  184.     // This routine sets the mouse position to the passed point
  185.     CrsrDevRec *firstMouse;
  186.  
  187.     if (TrapAvailable(_CrsrDevDispatch)) { 
  188.         // If we get here we have the CDM
  189.         firstMouse = NULL;    // start at head of cursor dev list
  190.         CrsrDevNextDevice(&firstMouse); // get the next cursor device
  191.         // Call CDM to move the mouse
  192.         CrsrDevMoveTo(firstMouse,(long)newPoint.h,(long)newPoint.v);
  193.     } else {
  194.         // No CDM so we use the low memory globals
  195.         *(Point *)xRawMouse = newPoint;
  196.         *(Point *)xMTemp = newPoint;
  197.         *(Ptr)xCrsrNew = *(Ptr)xCrsrCouple; // Set CrsrNew if coupled
  198.         CallCrsr();    // must call jCrsrTask to update system
  199.     }
  200. }
  201.  
  202. void SetMouseDevPosnRel(Point newPoint);
  203. void SetMouseDevPosnRel(Point newPoint)
  204. {
  205.     // Adds the passed point to the current mouse location
  206.     CrsrDevRec *firstMouse;
  207.     Point tempPt;
  208.     if (TrapAvailable(_CrsrDevDispatch)) { 
  209.         // If we get here we have the CDM
  210.         firstMouse = NULL;    // start at head of cursor dev list
  211.         CrsrDevNextDevice(&firstMouse);    // get the next cursor device
  212.         // Call CDM to move the mouse relative
  213.         CrsrDevMove(firstMouse,(long)newPoint.h,(long)newPoint.v);
  214.     } else {
  215.         // No CDM so we use the low memory globals
  216.         tempPt = *(Point *)xRawMouse;
  217.         tempPt.h += newPoint.h;
  218.         tempPt.v += newPoint.v;
  219.         *(Point *)xRawMouse = tempPt;
  220.         *(Point *)xMTemp = tempPt;
  221.         *(Ptr)xCrsrNew = *(Ptr)xCrsrCouple;
  222.         CallCrsr();
  223.     }
  224.     
  225. void FudgeCursor(void);
  226. void FudgeCursor(void)
  227. {
  228. // Now here is a routine that you can drop into the traffic light sample that
  229. // can be called to slowly move the mouse to the lower right hand corner of 
  230. // the screen it exits when the mouse button is held down
  231.  
  232.     Point RandPt;
  233.     long fred;
  234.  
  235.     GetMouseDevPosn(&RandPt);
  236.     do {
  237.     RandPt.h += 1; // Bump to the next pixel across
  238.     RandPt.v += 1; // Bump to the next pixel down
  239.     
  240.     SetMouseDevPosn(RandPt); // move absolute
  241.     
  242.     Delay(10,&fred); // wait 10 ticks for smooth drawing
  243.     } while (!Button()); // quit when the button is down.
  244. }
  245.  
  246. #endif // !__powerc